package net.tsz.afinal.download;

/**
 * Created by Jaelyn on 16/3/4.
 */
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import com.example.aymutilused2.R;
import net.tsz.afinal.sdcard.SdcardHelper;

import java.util.Timer;
import java.util.TimerTask;

public class DownloadAddQueueView extends Activity implements OnClickListener, ServiceConnection {
    private final String TAG = super.getClass().getSimpleName();
    private LinearLayout contentView;
    private String title;
    private DownLoadQueue queue;
    private int queue_id;
    private int queue_iconResId;
    private String queue_name;
    private String queue_downLoadUrl;
    private String queue_downLoadedPath;
    private boolean isToStop = false;
    private boolean isDownLoading = false;
    private String openFileType;
    private boolean downLoadedAutoOpenFile;
    private Button btn_stop;
    private Button btn_cancel;
    private DownloadService.DownloadServiceBinder binder;
    private TextView tv;
    private Timer timer;
    private TimerTask timerTask = new TimerTask() {
        public void run() {
            DownloadAddQueueView.this.handler.sendEmptyMessage(1);
        }
    };
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            DownloadAddQueueView.this.tv.setText(DownloadAddQueueView.this.queue.getName() + "\n" + DownloadAddQueueView.this.getString(2130968580) + ProgressGet100Util.get100_00Progress(DownloadAddQueueView.this.queue.getDownSize(), DownloadAddQueueView.this.queue.getOffSize()));
            if(DownloadAddQueueView.this.queue.isDownLoadOK()) {
                DownloadAddQueueView.this.finish();
            }

        }
    };

    public DownloadAddQueueView() {
    }

    protected void onCreate(Bundle savedInstanceState) {
        this.setTheme(android.R.style.Animation_Dialog);
        super.onCreate(savedInstanceState);
        if(!(new SdcardHelper()).ExistSDCard()) {
            this.finish();
        }

        try {
            Intent e = new Intent(this, DownloadService.class);
            this.getApplicationContext().startService(e);
            this.getApplicationContext().bindService(e, this, BIND_AUTO_CREATE);
        } catch (Exception var3) {

            this.finish();
            return;
        }

        this.getIntentData();
        if(this.queue == null) {

            this.finish();
        }

        this.setTitle(this.title == null?this.getString(R.string.text_tips):this.title);
        this.setContentView(this.creatContentView());
        if(!this.isToStop) {
            this.timer = new Timer();
            this.timer.schedule(this.timerTask, 100L, 300L);
        }

    }

    protected void onDestroy() {
        if(this.timer != null) {
            this.timer.cancel();
        }

        try {
            this.getApplicationContext().unbindService(this);
        } catch (Exception var2) {
        }

        super.onDestroy();
    }

    private void getIntentData() {
        Intent intent = this.getIntent();
        this.title = intent.getStringExtra("title");
        this.isToStop = intent.getBooleanExtra("isToStop", false);

        try {
            this.queue = (DownLoadQueue)intent.getExtras().get("queue");
        } catch (Exception var3) {
            this.queue = null;
        }

        if(this.queue == null) {
            this.queue_id = intent.getIntExtra("queue_id", 0);
            this.queue_iconResId = intent.getIntExtra("queue_iconResId", 0);
            this.queue_name = intent.getStringExtra("queue_name");
            this.queue_downLoadedPath = intent.getStringExtra("queue_downLoadedPath");
            this.queue_downLoadUrl = intent.getStringExtra("queue_downLoadUrl");
            this.downLoadedAutoOpenFile = intent.getBooleanExtra("queue_downLoadedAutoOpenFile", false);
            this.openFileType = intent.getStringExtra("queue_openFileType");
            if(this.queue_name == null || this.queue_downLoadedPath == null || this.queue_downLoadUrl == null || "".equals(this.queue_name) || "".equals(this.queue_downLoadedPath) || "".equals(this.queue_downLoadUrl) || this.queue_id <= 0) {
                return;
            }

            this.queue = new DownLoadQueue();
            this.queue.setId(this.queue_id);
            this.queue.setIconResId(this.queue_iconResId);
            this.queue.setCancel(false);
            this.queue.setDownLoadedPath(this.queue_downLoadedPath);
            this.queue.setDownLoadUrl(this.queue_downLoadUrl);
            this.queue.setName(this.queue_name);
            this.queue.setOpenFileType(this.openFileType);
            this.queue.setDownLoadedAutoOpenFile(this.downLoadedAutoOpenFile, this.openFileType);
        }

    }

    private View creatContentView() {
        this.contentView = new LinearLayout(this);
        this.contentView.setOrientation(LinearLayout.VERTICAL);
        LayoutParams lp = new LayoutParams(-1, -2);
        lp.setMargins(10, 10, 10, 10);
        this.tv = new TextView(this);
        LinearLayout ll = new LinearLayout(this);
        LayoutParams lp2 = new LayoutParams(-2, -2);
        lp2.weight = 1.0F;
        this.btn_stop = new Button(this);
        this.btn_stop.setOnClickListener(this);
        this.btn_cancel = new Button(this);
        this.btn_cancel.setOnClickListener(this);
        ll.addView(this.btn_stop, lp2);
        ll.addView(this.btn_cancel, lp2);
        this.contentView.addView(this.tv, lp);
        this.contentView.addView(ll, lp);
        if(this.isToStop) {
            if(this.queue.isDownLoadOK()) {
                this.tv.setText(this.queue.getName() + "\n" + this.getString(R.string.text_downLoadedfinish));
                this.btn_stop.setText(R.string.text_downLoaded_open);
            } else {
                this.tv.setText(this.queue.getName() + "\n" + this.getString(R.string.text_downLoad_stop));
                this.btn_stop.setText(R.string.text_downLoad_stop);
            }
            this.btn_cancel.setText(R.string.text_cancel);
        } else {
            this.tv.setText(this.queue.getName() + "\n" + this.getString(R.string.text_downLoaded) + ProgressGet100Util.get100_00Progress(this.queue.getDownSize(), this.queue.getOffSize()));
            this.btn_stop.setText(R.string.text_downLoad_background);
            this.btn_cancel.setText(R.string.text_cancel);
        }

        return this.contentView;
    }

    public void onClick(View v) {
        if(v.equals(this.btn_stop)) {
            if(this.isToStop) {
                if(this.queue.isDownLoadOK()) {
                    this.queue.OpenFile(this);
                    this.finish();
                } else {
                    this.queue.setCancel(true);
                    if(this.binder != null) {
                        this.binder.getService().stopQueue(this.queue.getId());
                    }

                    this.finish();
                }
            } else {
                this.finish();
            }
        } else if(this.isToStop) {
            this.finish();
        } else {
            this.queue.setCancel(true);
            this.finish();
        }

    }

    public void onServiceConnected(ComponentName name, IBinder service) {
        this.binder = (DownloadService.DownloadServiceBinder)service;
        if(!this.isToStop) {
            this.queue = this.binder.getService().addQueue(this.queue);
        }

    }

    public void onServiceDisconnected(ComponentName name) {

    }
}
